home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Resources / Internet / Maxthon 1.5.7 build 82 / mcombo.exe / Template / StartPage / js / feed.js < prev    next >
Text File  |  2005-12-08  |  4KB  |  126 lines

  1. ////////////////////////////////////////////////////////////////////////
  2. // Simple Feed Parser
  3. ////////////////////////////////////////////////////////////////////////
  4. // Support RSS 0.9x/1.0/2.0 & Atom 0.3/1.0
  5. //
  6. // Version: 1.5
  7. // Author: SiC
  8. // Last Modified: 2005-12-09 10:08:21
  9. ////////////////////////////////////////////////////////////////////////
  10.  
  11. var simpleFeedParser={
  12.  
  13.   // Member Variables
  14.   error: null,
  15.  
  16.   // *** Get a XMLDOM object ***
  17.   getXMLDOM: function(){
  18.     var objXMLDOM;
  19.     try{
  20.       objXMLDOM = new ActiveXObject("Microsoft.XMLDOM");
  21.       objXMLDOM.async = false;
  22.     }catch(e){
  23.       this.error = e;
  24.       return null;
  25.     }
  26.     return objXMLDOM;
  27.   },
  28.  
  29.   // *** Parse feed xml ***
  30.   parse: function(xml){
  31.     var objXMLDOM, xslDOM;
  32.     objXMLDOM=this.getXMLDOM();
  33.     if(!objXMLDOM){
  34.       return this.error.number+":"+simpleAjax.error.description;
  35.     }
  36.     objXMLDOM.loadXML(xml);
  37.     if(objXMLDOM.parseError.errorCode!=0){
  38.       return objXMLDOM.parseError.errorCode+':'+objXMLDOM.parseError.reason;
  39.     }else{
  40.       // RSS 0.9x/2.0 Feed
  41.       var result=this.parseFeedNodes(objXMLDOM,
  42.             ["/rss",
  43.              "/rss/channel/link",
  44.              "/rss/channel/title",
  45.              "//item",
  46.              "title",
  47.              "link",
  48.              "description"]);
  49.       if(result) return result;
  50.  
  51.       // RSS 1.0 Feed
  52.       var result=this.parseFeedNodes(objXMLDOM,
  53.             ["/rdf:RDF",
  54.              "//rss1:channel/rss1:link",
  55.              "//rss1:channel/rss1:title",
  56.              "//rss1:item",
  57.              "rss1:title",
  58.              "rss1:link",
  59.              "rss1:description"]);
  60.       if(result) return result;
  61.  
  62.       // Atom 0.3 Feed
  63.       var result=this.parseFeedNodes(objXMLDOM,
  64.             ["/atom03:feed",
  65.              "/atom03:feed/atom03:link[@rel='alternate' and @type='text/html']/@href",
  66.              "/atom03:feed/atom03:title",
  67.              "//atom03:entry",
  68.              "atom03:title",
  69.              ['atom03:link[@rel="alternate" and @type="text/html"]/@href','atom03:link[@rel="alternate"]/@href'],
  70.              ["atom03:content","atom03:summary"]
  71.             ]);
  72.       if(result) return result;
  73.  
  74.       // Atom 1.0 Feed
  75.       var result=this.parseFeedNodes(objXMLDOM,
  76.             ["/atom:feed",
  77.              "/atom:feed/atom:link[@rel='alternate' and @type='text/html']/@href",
  78.              "/atom:feed/atom:title",
  79.              "//atom:entry",
  80.              "atom:title",
  81.              ['atom:link[@rel="alternate" and @type="text/html"]/@href','atom:link[@rel="alternate"]/@href'],
  82.              ["atom:content","atom:summary"]
  83.             ]);
  84.       if(result) return result;
  85.  
  86.       return 'Unknown Feed Format'
  87.     }
  88.   },
  89.  
  90.   // *** Common Function for parse feed nodes ***
  91.   parseFeedNodes: function(objXMLDOM,arrParams){
  92.     objXMLDOM.setProperty("SelectionLanguage", "XPath");
  93.     objXMLDOM.setProperty("SelectionNamespaces", 'xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:atom03="http://purl.org/atom/ns#"  xmlns:rss1="http://purl.org/rss/1.0/"');
  94.     if(objXMLDOM.documentElement.selectSingleNode(arrParams[0])){
  95.       var arrItems=new Array();
  96.       var feedlink=this.parseSingleFeedNode(objXMLDOM, arrParams[1]);
  97.       var feedtitle=this.parseSingleFeedNode(objXMLDOM, arrParams[2]);
  98.  
  99.       var nodes=objXMLDOM.documentElement.selectNodes(arrParams[3]);
  100.       if(nodes.length>0){
  101.         for(var i=0;i<nodes.length;i++){
  102.           var link=this.parseSingleFeedNode(nodes[i], arrParams[5]);
  103.           var title=this.parseSingleFeedNode(nodes[i], arrParams[4]);
  104.           var description=this.parseSingleFeedNode(nodes[i], arrParams[6]);
  105.           if(title==""){ (link!="") ? title=link : "" };
  106.           if(title!=""||link!="") arrItems.push({"title":title, "link":link, "description":description});
  107.         }
  108.       }
  109.       return {"title":feedtitle, "link":feedlink, "items":arrItems};
  110.     }else{
  111.       return null;
  112.     }
  113.   },
  114.  
  115.   parseSingleFeedNode: function(node, arrParams){
  116.     if(typeof(arrParams)=="string") arrParams=new Array(arrParams);
  117.     for(var i=0;i<arrParams.length;i++){
  118.       var item=node.selectSingleNode(arrParams[i]);
  119.       if(item){
  120.         return item.text;
  121.       }
  122.     }
  123.     return "";
  124.   }
  125.  
  126. }